BDBPATH=/usr/local/BerkeleyDB.5.2
CC=g++
CFLAGS=-g -Wall -O3 -Wno-write-strings -fPIC -isystem $(BDBPATH)/include
LDFLAG=   -L $(BDBPATH)/lib -ldb -lz
OBJ=bdbibd.o xstdlib.o xstring.o xbdb.o linkage.o xzlib.o xstdio.o genomicpos.o ibdtrace.o postscript.o cgi.o
all:testread
DBNEW=/BELENOS/data/project/20110922ibdbrugada/dbnew/_ignore.backup

#R:rextension
#	${R_HOME}/bin/R --no-save < bdbibd.R
#rextension:bdbibdR.so
#bdbibdR.so: bdbibdRExt.o bdbibd.o xstdlib.o xbdb.o linkage.o xzlib.o xstdio.o 
#	$(CC) -shared -Wl,-soname,$@.1 -o $@ bdbibdRExt.o bdbibd.o xstdlib.o xbdb.o linkage.o  xzlib.o xstdio.o  -ldb -lz

testtrace: ./ibdtool
	$<  trace --db-home $(DBNEW) --family BrS17 --individual R1058 > jeter.ps
	ps2pdf jeter.ps


testbuild: ./ibdtool
	mkdir -p $(DBNEW)
	$< build \
		--db-home $(DBNEW) \
		--data data/ibdpath.txt \
		--map data/Axiom_GW_Hu_SNP.r2.na30.annot.csv.gz \
		--reskin /BELENOS/data/project/20110922ibdbrugada/data/_ignore.backup/ibdldreskin_brugada7 \
		--ped /BELENOS/data/project/20110922ibdbrugada/data/_ignore.backup/brugada7.fam

testbuildold: ./ibdtool
	mkdir -p /BELENOS/data/project/20110922ibdbrugada/dbnew/_ignore.backup
	$< build \
		--db-home /BELENOS/data/project/20110922ibdbrugada/dbnew/_ignore.backup \
		--data data/ibdpath.txt \
		--map data/Axiom_GW_Hu_SNP.r2.na30.annot.csv.gz \
		--reskin data/ibdldreskin \
		--ped /BELENOS/data/user/cdina/PROGRAMMES/IBDLD/IBDLDv1.18/Mon_Example/bruga2.fam
testread: ./ibdtool
	$< read \
		--db-home $(DBNEW) --restrict-family BrS13 data/rs_list.txt
./ibdtool :main.o $(OBJ)
	$(CC) -o $@  main.o $(OBJ) $(LDFLAG) 

cgi:bdbibd.cgi
	cp $< /var/www/cgi-bin/bdbibd/
bdbibd.cgi:ibdcgi.o $(OBJ)
	$(CC) -o $@ ibdcgi.o $(OBJ) $(LDFLAG) 
ibdscanner:ibdscanner.o $(OBJ)
	$(CC) -o $@ ibdscanner.o $(OBJ) $(LDFLAG) 
ibdcomm:ibdcomm.o $(OBJ)
	$(CC) -o $@ ibdcomm.o $(OBJ) $(LDFLAG) 
#bdbibdRExt.o:bdbibdRExt.cpp bdbibd.h xbdb.h 
#	$(CC) -I ${R_HOME}/include -c -o $@ $(CFLAGS) $<	
main.o:main.cpp bdbibd.h xbdb.h ibdtrace.h
	$(CC) -c -o $@ $(CFLAGS) $<	
bdbibd.o:bdbibd.cpp bdbibd.h xbdb.h linkage.h
	$(CC) -c -o $@ $(CFLAGS) $<
ibdtrace.o:ibdtrace.cpp bdbibd.h ibdtrace.h
	$(CC) -c -o $@ $(CFLAGS) $<
xstdlib.o:xstdlib.cpp xstdlib.h
	$(CC) -c -o $@ $(CFLAGS) $<
xzlib.o:xzlib.cpp xstdlib.h xstdio.h
	$(CC) -c -o $@ $(CFLAGS) $<
xstdio.o:xstdio.cpp xstdio.h xstdio.h
	$(CC) -c -o $@ $(CFLAGS) $<
xstring.o:xstring.cpp xstring.h xstdio.h
	$(CC) -c -o $@ $(CFLAGS) $<
xbdb.o:xbdb.cpp xbdb.h xstdlib.h
	$(CC) -c -o $@ $(CFLAGS) $<
linkage.o:linkage.cpp linkage.h
	$(CC) -c -o $@ $(CFLAGS) $<
genomicpos.o:genomicpos.cpp genomicpos.h
	$(CC) -c -o $@ $(CFLAGS) $<
postscript.o:postscript.cpp postscript.h
	$(CC) -c -o $@ $(CFLAGS) $<	
cgi.o:cgi.cpp cgi.h
	$(CC) -c -o $@ $(CFLAGS) $<	
ibdcgi.o:ibdcgi.cpp 
	$(CC) -c -o $@ $<  $(CFLAGS) 
ibdscanner.o:ibdscanner.cpp
	$(CC) -c -o $@ $<  $(CFLAGS) 
ibdcomm.o:ibdcomm.cpp
	$(CC) -c -o $@ $<  $(CFLAGS) 
archive:
	tar cvfz archive.tar.gz Makefile *.cpp *.h
clean:
	rm -f *.o *~ a.out err
clean-db:
	rm -f $(DBNEW)/*
data-test:
	rm -f data/test.annot.csv.gz
	gunzip -c /BELENOS/data/user/cdina/PROGRAMMES/IBDLD/IBDLDv1.18/Mon_Example/TSF/brs17.ibd.gz | head -n 1000 > data/test.ibd
	rm -f data/test.ibd.gz
	gzip data/test.ibd
	gunzip -c data/test.ibd.gz | cut -d '	' -f 5 | sort | uniq > _markers
	gunzip -c data/Axiom_GW_Hu_SNP.r2.na31.annot.csv.gz | head -n 1000 | egrep "^#" > data/test.annot.csv
	gunzip -c data/Axiom_GW_Hu_SNP.r2.na31.annot.csv.gz | head -n 1000 | egrep "Heterozygous" >> data/test.annot.csv
	gunzip -c data/Axiom_GW_Hu_SNP.r2.na31.annot.csv.gz | grep  -w -f _markers >> data/test.annot.csv
	rm -f data/test.annot.csv.gz
	gzip data/test.annot.csv
